//package com.shanzhu.book.mq;
//
//import com.shanzhu.book.config.RabbitMQConfig;
//import com.shanzhu.book.mapper.SeatReservationMapper;
//import com.shanzhu.book.model.SeatReservation;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//@Service
//public class SeatMessageConsumer2 {
//
//    private static final Logger logger = LoggerFactory.getLogger(SeatMessageConsumer2.class);
//
//    @Autowired
//    private SeatReservationMapper seatReservationMapper;
//
//    @RabbitListener(queues = RabbitMQConfig.SEAT_RESERVATION_QUEUE)
//    public void handleReservation(SeatReservation reservation) {
//        try {
//            logger.info("Processing seat reservation: {}", reservation);
//            // 检查座位是否可用
//            int count = seatReservationMapper.checkSeatAvailability(
//                reservation.getSeatNumber(),
//                reservation.getStartTime(),
//                reservation.getEndTime());
//
//            if (count == 0) {
//                reservation.setReserveStatus(1); // 已预约状态
//                seatReservationMapper.insertSelective(reservation);
//                logger.info("Seat reserved successfully: {}", reservation.getId());
//            } else {
//                logger.warn("Seat not available for reservation: {}", reservation);
//            }
//        } catch (Exception e) {
//            logger.error("Error processing seat reservation", e);
//            // 可以添加重试逻辑或错误处理
//        }
//    }
//
//    @RabbitListener(queues = RabbitMQConfig.SEAT_CANCEL_QUEUE)
//    public void handleCancel(Long reservationId) {
//        try {
//            logger.info("Processing seat cancellation: {}", reservationId);
//            SeatReservation reservation = seatReservationMapper.selectByPrimaryKey(reservationId);
//
//            if (reservation != null && reservation.getReserveStatus() == 1) {
//                seatReservationMapper.deleteByPrimaryKey(reservationId);
//                logger.info("Seat reservation cancelled: {}", reservationId);
//            } else {
//                logger.warn("Cannot cancel reservation, status invalid or not found: {}", reservationId);
//            }
//        } catch (Exception e) {
//            logger.error("Error processing seat cancellation", e);
//        }
//    }
//}